package models

import (
	"encoding/json"
	"mindonmap/pkgs/util"
	"time"

	"github.com/jinzhu/gorm"
)

// 结构
type MindVersion struct {
	Model
	Acid        int    `json:"acid"`
	Pid         string `json:"pid"`
	State       int    `json:"-"`
	Mapdata     string `json:"mapdata"`
	Image       string `json:"image"`
	Version     int    `json:"version"`
	Createtime  int    `json:"createtime"`
	Modifietime int    `json:"modifietime"`
}

func (tag *MindVersion) BeforeCreate(scope *gorm.Scope) error {
	scope.SetColumn("createtime", time.Now().Unix())
	return nil
}

func (tag *MindVersion) BeforeUpdate(scope *gorm.Scope) error {
	scope.SetColumn("modifietime", time.Now().Unix())
	return nil
}

func GetMindVersion(id int, version int) (minds MindVersion) {
	db.Where("version = ? and  id = ?", version, id).Order("id desc").First(&minds)
	//db.Model(&minds).Related(&minds.Tag)
	return
}
func GetFlowVersion_back(id int, version int) (minds MindVersion) {
	db.Where("version = ? and  id = ?", version, id).Order("id desc").First(&minds)
	//db.Model(&minds).Related(&minds.Tag)
	return
}

func GetMindVersions(PageNum int, PageSize int, id string) (minds []MindVersion) {
	db.Where("pid = ?", id).Offset(PageNum).Limit(PageSize).Order("id desc").Find(&minds)
	return minds
}

func GetMindVersionsAll(PageNum int, PageSize int, id string) (minds []MindVersion) {
	db.Where("version in (1,2) and  pid = ?", id).Offset(PageNum).Limit(PageSize).Order("version asc,id desc").Find(&minds)
	return minds
}

func AddMindVersion(data map[string]interface{}) bool {
	db.Create(&MindVersion{
		Acid:    data["acid"].(int),
		Pid:     data["pid"].(string),
		Mapdata: data["mapdata"].(string),
		State:   data["state"].(int),
		Image:   data["image"].(string),
		Version: data["version"].(int),
	})
	newValue, _ := json.Marshal(data)
	util.WriteLog(" INSERT INTO `md_mind_version` (`acid`,`pid`,`state`,`mapdata`,`image`,`version`,`createtime`,`modifietime`) VALUES (" + string(newValue) + ")")
	return true
}

func EditMindVersion(id string, maps interface{}) bool {
	db.Model(&MindVersion{}).Where("version = 2 and pid = ?", id).Update(maps)
	return true
}
func EditMindVersionSource(id string, maps interface{}) bool {
	db.Model(&MindVersion{}).Where("version = 1 and pid = ?", id).Update(maps)
	return true
}

func DeleteMindVersion(id int, version int) bool {
	db.Where(" version = ? and  id = ?", version, id).Delete(&MindVersion{})
	return true
}

func ExistMindVersionByID(id int, version int) bool {
	var minds MindVersion
	db.Where("version = ? and  id = ?", version, id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func ExistMindVersionBySourceID(id string) bool {
	var minds MindVersion
	db.Where("version = 1 and pid = ?", id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func ExistMindVersionByMindID(id string) bool {
	var count int
	db.Model(&MindVersion{}).Where("version = 2 and pid = ?", id).Count(&count)
	if count > 1 {
		return true
	}
	return false
}

func GetMindVersionID(id int, version int) bool {
	var minds MindVersion
	db.Where("version = ? and  id = ?", version, id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func GetMindVersionTotal(maps interface{}) (count int) {
	db.Model(&MindVersion{}).Where(maps).Count(&count)
	return
}
